﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类Community_BaseInfo。
    /// </summary>
    public class Community_BaseInfo
    {
        public Community_BaseInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string ColumnName,string Value)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            //StringBuilder strSql = new StringBuilder();
            string strSql = string.Format("select count(1) from Community_BaseInfo where {0}='{1}'", ColumnName, Value);
            //strSql.Append("select count(1) from Community_BaseInfo where @ColumnName='@Value' ");
            //DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            //db.AddInParameter(dbCommand, "ColumnName", DbType.String, ColumnName);
            //db.AddInParameter(dbCommand, "Value", DbType.String, Value);            
            DbCommand dbCommand = db.GetSqlStringCommand(strSql);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string strwhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            string strSql = string.Format("select count(1) from Community_BaseInfo where {0}", strwhere);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.Community_BaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Community_BaseInfo(");
            strSql.Append("CommunityId,CommunityName,CityId,CityName,CompanyId,CompanyName,Region,Block,Address,Address2,Address3,Address4,CommunityUse,BuildingType,CompleteYear,Schools,Parks,Traffics,Description,AddTime,AddPerson,PY,Status,CommunityBlock)");

            strSql.Append(" values (");
            strSql.Append("@CommunityId,@CommunityName,@CityId,@CityName,@CompanyId,@CompanyName,@Region,@Block,@Address,@Address2,@Address3,@Address4,@CommunityUse,@BuildingType,@CompleteYear,@Schools,@Parks,@Traffics,@Description,@AddTime,@AddPerson,@PY,@Status,@CommunityBlock)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "CommunityName", DbType.AnsiString, model.CommunityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Region", DbType.AnsiString, model.Region);
            db.AddInParameter(dbCommand, "Block", DbType.AnsiString, model.Block);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Address2", DbType.AnsiString, model.Address2);
            db.AddInParameter(dbCommand, "Address3", DbType.AnsiString, model.Address3);
            db.AddInParameter(dbCommand, "Address4", DbType.AnsiString, model.Address4);
            db.AddInParameter(dbCommand, "CommunityUse", DbType.AnsiString, model.CommunityUse);
            db.AddInParameter(dbCommand, "BuildingType", DbType.AnsiString, model.BuildingType);
            db.AddInParameter(dbCommand, "CompleteYear", DbType.Int32, model.CompleteYear);
            db.AddInParameter(dbCommand, "Schools", DbType.AnsiString, model.Schools);
            db.AddInParameter(dbCommand, "Parks", DbType.AnsiString, model.Parks);
            db.AddInParameter(dbCommand, "Traffics", DbType.AnsiString, model.Traffics);
            db.AddInParameter(dbCommand, "Description", DbType.AnsiString, model.Description);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "PY", DbType.AnsiString, model.PY);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "CommunityBlock", DbType.AnsiString, model.CommunityBlock);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.Community_BaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Community_BaseInfo set ");
            strSql.Append("CommunityName=@CommunityName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("Region=@Region,");
            strSql.Append("Block=@Block,");
            strSql.Append("Address=@Address,");
            strSql.Append("Address2=@Address2,");
            strSql.Append("Address3=@Address3,");
            strSql.Append("Address4=@Address4,");
            strSql.Append("CommunityUse=@CommunityUse,");
            strSql.Append("BuildingType=@BuildingType,");
            strSql.Append("CompleteYear=@CompleteYear,");
            strSql.Append("Schools=@Schools,");
            strSql.Append("Parks=@Parks,");
            strSql.Append("Traffics=@Traffics,");
            strSql.Append("Description=@Description,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("PY=@PY,");
            strSql.Append("Status=@Status,");
            strSql.Append("CommunityBlock=@CommunityBlock");
            strSql.Append(" where CommunityId=@CommunityId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "CommunityName", DbType.AnsiString, model.CommunityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Region", DbType.AnsiString, model.Region);
            db.AddInParameter(dbCommand, "Block", DbType.AnsiString, model.Block);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Address2", DbType.AnsiString, model.Address2);
            db.AddInParameter(dbCommand, "Address3", DbType.AnsiString, model.Address3);
            db.AddInParameter(dbCommand, "Address4", DbType.AnsiString, model.Address4);
            db.AddInParameter(dbCommand, "CommunityUse", DbType.AnsiString, model.CommunityUse);
            db.AddInParameter(dbCommand, "BuildingType", DbType.AnsiString, model.BuildingType);
            db.AddInParameter(dbCommand, "CompleteYear", DbType.Int32, model.CompleteYear);
            db.AddInParameter(dbCommand, "Schools", DbType.AnsiString, model.Schools);
            db.AddInParameter(dbCommand, "Parks", DbType.AnsiString, model.Parks);
            db.AddInParameter(dbCommand, "Traffics", DbType.AnsiString, model.Traffics);
            db.AddInParameter(dbCommand, "Description", DbType.AnsiString, model.Description);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "PY", DbType.AnsiString, model.PY);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "CommunityBlock", DbType.AnsiString, model.CommunityBlock);
            db.ExecuteNonQuery(dbCommand);

        }



        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="buildingId"></param>
        /// <param name="status"></param>
        public static void UpdateStatus(Guid communityId, int status)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Community_BaseInfo set ");
            strSql.Append("Status=@Status");
            strSql.Append(" where CommunityId=@CommunityId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, communityId);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, status);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid CommunityId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Community_BaseInfo ");
            strSql.Append(" where CommunityId=@CommunityId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, CommunityId);
            db.ExecuteNonQuery(dbCommand);

        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void DelCommunity(Guid CommunityId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Community_BaseInfo ");
            strSql.Append(" where CommunityId=@CommunityId");
            strSql.Append(" delete from Community_BuildingInfo ");
            strSql.Append(" where CommunityId=@CommunityId");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, CommunityId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.Community_BaseInfo GetModel(Guid CommunityId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CityId,CityName,CompanyId,CompanyName,CommunityId,CommunityName,Region,Block,Address,Address2,Address3,Address4,CommunityUse,BuildingType,CompleteYear,Schools,Parks,Traffics,Description,AddTime,AddPerson,PY,Status,CommunityBlock from Community_BaseInfo ");
            strSql.Append(" where CommunityId=@CommunityId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, CommunityId);
            Chenxin.Lib.House.Entity.Community_BaseInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获取列表数据根据查询条件
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns></returns>
        public static DataSet GetList(string strWhere,string orderBy)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CommunityId,CommunityName,Region,Block,Address,Address2,Address3,Address4,CommunityUse,BuildingType,CompleteYear,Schools,Parks,Traffics,Description,AddTime,AddPerson,PY,Status,CommunityBlock ");
            strSql.Append(" FROM Community_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (orderBy.Trim() != "")
            {
                strSql.Append(" order by " + orderBy);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取列表数据根据查询条件
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderBy">排序字段</param>
        /// <returns></returns>
        public static DataSet GetList(string strWhere, string orderBy,int topCont)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select top({0}) CommunityId,CommunityName,CityId,CityName,CompanyId,CompanyName,Region,Block,Address,Address2,Address3,Address4,CommunityUse,BuildingType,CompleteYear,Schools,Parks,Traffics,Description,AddTime,AddPerson,PY,Status,CommunityBlock ", topCont);
            strSql.Append(" FROM Community_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (orderBy.Trim() != "")
            {
                strSql.Append(" order by " + orderBy);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CommunityId,CommunityName,Region,Block,Address,Address2,Address3,Address4,CommunityUse,BuildingType,CompleteYear,Schools,Parks,Traffics,Description,AddTime,AddPerson,PY,Status,CommunityBlock ");
            strSql.Append(" FROM Community_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Community_BaseInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CommunityId,CommunityName,Region,Block,Address,Address2,Address3,Address4,CommunityUse,BuildingType,CompleteYear,Schools,Parks,Traffics,Description,AddTime,AddPerson,PY,Status,CommunityBlock ");
            strSql.Append(" FROM Community_BaseInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.Community_BaseInfo> list = new List<Chenxin.Lib.House.Entity.Community_BaseInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Community_BaseInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.Community_BaseInfo model = new Chenxin.Lib.House.Entity.Community_BaseInfo();
            object ojb;
            ojb = dataReader["CommunityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CommunityId = new Guid(ojb.ToString());
            }

            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();

            model.CommunityName = dataReader["CommunityName"].ToString();
            model.Region = dataReader["Region"].ToString();
            model.Block = dataReader["Block"].ToString();
            model.Address = dataReader["Address"].ToString();
            model.Address2 = dataReader["Address2"].ToString();
            model.Address3 = dataReader["Address3"].ToString();
            model.Address4 = dataReader["Address4"].ToString();
            model.CommunityUse = dataReader["CommunityUse"].ToString();
            model.BuildingType = dataReader["BuildingType"].ToString();
            ojb = dataReader["CompleteYear"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompleteYear = (int)ojb;
            }
            model.Schools = dataReader["Schools"].ToString();
            model.Parks = dataReader["Parks"].ToString();
            model.Traffics = dataReader["Traffics"].ToString();
            model.Description = dataReader["Description"].ToString();
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            model.PY = dataReader["PY"].ToString();
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            model.CommunityBlock = dataReader["CommunityBlock"].ToString();
            return model;
        }

        /// <summary>
        /// 获得楼盘民称（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Community_BaseInfo> GetListArray(string sql, System.Collections.Generic.List<string> stList)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            db.AddInParameter(dbCommand, "CommunityName", DbType.String, stList[0]);
            db.AddInParameter(dbCommand, "Py", DbType.String, stList[1]);
            List<Entity.Community_BaseInfo> list = new List<Entity.Community_BaseInfo>();

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    list.Add(subReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 对象实体绑定部分数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Community_BaseInfo subReaderBind(IDataReader dataReader)
        {
            Entity.Community_BaseInfo model = new Entity.Community_BaseInfo();
            object ojb;
            ojb = dataReader["CommunityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CommunityId = new Guid(ojb.ToString());
            }
            model.CommunityName = dataReader["CommunityName"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

